<?php
/**
 * Created by PhpStorm.
 * User: fengyan
 * Date: 2017/10/28
 * Time: 上午9:11
 */

namespace App\Repositories\Admin;

use App\Models;
use Illuminate\Support\Facades\DB;

class PermissionRepository extends AdminRepository
{

    public function __construct()
    {
        parent::__construct();
        $this->model = new Models\Admin\Permission();
    }

    private static $instance;

    /**
     * 实现单例
     */
    public static function getInstance()
    {
        if (!(self::$instance instanceof self)) {
            self::$instance = new self();
        }

        return self::$instance;
    }

    /**
     * 声明实例模型
     * @param array $filter
     * @return mixed
     */
    public function getSelectModel(array $filter = []) : \Illuminate\Database\Eloquent\Builder
    {

    }

    /**
     * 各个服务需实现自己的分页方法
     * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
     */
    public function getPaginateData(): \Illuminate\Contracts\Pagination\LengthAwarePaginator
    {
        // TODO: Implement getPaginateData() method.
    }

    /**
     * 根据用户编号获取权限名称
     * @param $uid
     * @return array
     */
    public function getNameListBuUserId($uid) : array
    {
        return DB::table('admin_permissions')
            ->join('admin_permission_role', 'admin_permissions.id', '=', 'admin_permission_role.permission_id')
            ->join('admin_role_user', 'admin_permission_role.role_id', '=', 'admin_role_user.role_id')
            ->where('admin_role_user.user_id', '=', $uid)->pluck('admin_permissions.name')->toArray();
    }
}